const Define = require('./ExportDefine')
var coder = require('../public/coder')
var file_util = require('../public/file_util');
const path = require('path');

function ClientExport(configObj) {
    this.configObj = configObj;
}
ClientExport.prototype.getResult = function (result) {
    return JSON.stringify(coder.pack(result, this.configObj.isEncode))
}
ClientExport.prototype.export = function (sheetList, head, dir) {
    let bigFileName = head.bigFileName
    if (bigFileName && bigFileName != "null") {
        var filename = path.join(dir.json_path, bigFileName + ".json");
        var bigObj = {};
        bigObj["data"] = {};
        for (let index = 0; index < sheetList.length; index++) {
            const sheet = sheetList[index];
            bigObj["data"][sheet.sheetName] = sheet.result;
        }
        bigObj["type"] = Define.TEMPLATE;
        let out = this.getResult(bigObj)
        file_util.writeFile(filename, out);
    } else {
        for (let index = 0; index < sheetList.length; index++) {
            const sheet = sheetList[index];
            var filename = path.join(dir.json_path, sheet.sheetName + ".json");
            file_util.writeFile(filename, this.getResult(sheet.result));
        }
    }
    // manager.export(managerTemplate)
    console.log("qqq");
}
module.exports = ClientExport; 
